package com.kenai.jbosh;

import TT;;
import java.io.Closeable;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

final class ServiceLib
{
  private static final Logger LOG = Logger.getLogger(ServiceLib.class.getName());

  private static <T> T attemptLoad(Class<T> paramClass, String paramString)
  {
    Level localLevel;
    Object localObject1;
    Object localObject2;
    if (LOG.isLoggable(Level.FINEST))
      LOG.finest("Attempting service load: " + paramString);
    try
    {
      Class localClass = Class.forName(paramString);
      if (!(paramClass.isAssignableFrom(localClass)))
      {
        if (LOG.isLoggable(Level.WARNING))
          LOG.warning(localClass.getName() + " is not assignable to " + paramClass.getName());
      }
      else
      {
        Object localObject3 = paramClass.cast(localClass.newInstance());
        localObject2 = localObject3;
      }
    }
    catch (LinkageError localLinkageError)
    {
      localLevel = Level.FINEST;
      localObject1 = localLinkageError;
      LOG.log(localLevel, "Could not load " + paramClass.getSimpleName() + " instance: " + paramString, (Throwable)localObject1);
      localObject2 = null;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      while (true)
      {
        localLevel = Level.FINEST;
        localObject1 = localClassNotFoundException;
      }
    }
    catch (InstantiationException localInstantiationException)
    {
      while (true)
      {
        localLevel = Level.WARNING;
        localObject1 = localInstantiationException;
      }
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      while (true)
      {
        localLevel = Level.WARNING;
        localObject1 = localIllegalAccessException;
      }
      localObject2 = null;
    }
    return ((TT)localObject2);
  }

  private static void finalClose(Closeable paramCloseable)
  {
    if (paramCloseable != null);
    try
    {
      paramCloseable.close();
      return;
    }
    catch (IOException localIOException)
    {
      LOG.log(Level.FINEST, "Could not close: " + paramCloseable, localIOException);
    }
  }

  static <T> T loadService(Class<T> paramClass)
  {
    Object localObject;
    Iterator localIterator = loadServicesImplementations(paramClass).iterator();
    do
    {
      if (!(localIterator.hasNext()))
        throw new IllegalStateException("Could not load " + paramClass.getName() + " implementation");
      localObject = attemptLoad(paramClass, (String)localIterator.next());
    }
    while (localObject == null);
    if (LOG.isLoggable(Level.FINEST))
      LOG.finest("Selected " + paramClass.getSimpleName() + " implementation: " + localObject.getClass().getName());
    return localObject;
  }

  // ERROR //
  private static List<String> loadServicesImplementations(Class paramClass)
  {
    // Byte code:
    //   0: new 155	java/util/ArrayList
    //   3: dup
    //   4: invokespecial 156	java/util/ArrayList:<init>	()V
    //   7: astore_1
    //   8: aload_0
    //   9: invokevirtual 14	java/lang/Class:getName	()Ljava/lang/String;
    //   12: invokestatic 162	java/lang/System:getProperty	(Ljava/lang/String;)Ljava/lang/String;
    //   15: astore_2
    //   16: aload_2
    //   17: ifnull +11 -> 28
    //   20: aload_1
    //   21: aload_2
    //   22: invokeinterface 166 2 0
    //   27: pop
    //   28: ldc 2
    //   30: invokevirtual 170	java/lang/Class:getClassLoader	()Ljava/lang/ClassLoader;
    //   33: new 47	java/lang/StringBuilder
    //   36: dup
    //   37: ldc 172
    //   39: invokespecial 52	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   42: aload_0
    //   43: invokevirtual 14	java/lang/Class:getName	()Ljava/lang/String;
    //   46: invokevirtual 56	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   49: invokevirtual 59	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   52: invokevirtual 178	java/lang/ClassLoader:getResource	(Ljava/lang/String;)Ljava/net/URL;
    //   55: astore_3
    //   56: aload_3
    //   57: ifnonnull +5 -> 62
    //   60: aload_1
    //   61: areturn
    //   62: aconst_null
    //   63: astore 4
    //   65: aconst_null
    //   66: astore 5
    //   68: aconst_null
    //   69: astore 6
    //   71: aload_3
    //   72: invokevirtual 184	java/net/URL:openStream	()Ljava/io/InputStream;
    //   75: astore 4
    //   77: new 186	java/io/InputStreamReader
    //   80: dup
    //   81: aload 4
    //   83: invokespecial 189	java/io/InputStreamReader:<init>	(Ljava/io/InputStream;)V
    //   86: astore 10
    //   88: new 191	java/io/BufferedReader
    //   91: dup
    //   92: aload 10
    //   94: invokespecial 194	java/io/BufferedReader:<init>	(Ljava/io/Reader;)V
    //   97: astore 11
    //   99: aload 11
    //   101: invokevirtual 197	java/io/BufferedReader:readLine	()Ljava/lang/String;
    //   104: astore 13
    //   106: aload 13
    //   108: ifnonnull +21 -> 129
    //   111: aload 11
    //   113: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   116: aload 10
    //   118: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   121: aload 4
    //   123: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   126: goto -66 -> 60
    //   129: aload 13
    //   131: ldc 201
    //   133: invokevirtual 205	java/lang/String:matches	(Ljava/lang/String;)Z
    //   136: ifne -37 -> 99
    //   139: aload_1
    //   140: aload 13
    //   142: invokevirtual 208	java/lang/String:trim	()Ljava/lang/String;
    //   145: invokeinterface 166 2 0
    //   150: pop
    //   151: goto -52 -> 99
    //   154: astore 12
    //   156: aload 12
    //   158: astore 8
    //   160: aload 11
    //   162: astore 6
    //   164: aload 10
    //   166: astore 5
    //   168: getstatic 22	com/kenai/jbosh/ServiceLib:LOG	Ljava/util/logging/Logger;
    //   171: getstatic 73	java/util/logging/Level:WARNING	Ljava/util/logging/Level;
    //   174: new 47	java/lang/StringBuilder
    //   177: dup
    //   178: ldc 210
    //   180: invokespecial 52	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   183: aload_3
    //   184: invokevirtual 211	java/net/URL:toString	()Ljava/lang/String;
    //   187: invokevirtual 56	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   190: invokevirtual 59	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   193: aload 8
    //   195: invokevirtual 103	java/util/logging/Logger:log	(Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
    //   198: aload 6
    //   200: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   203: aload 5
    //   205: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   208: aload 4
    //   210: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   213: goto -153 -> 60
    //   216: astore 9
    //   218: aload 6
    //   220: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   223: aload 5
    //   225: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   228: aload 4
    //   230: invokestatic 199	com/kenai/jbosh/ServiceLib:finalClose	(Ljava/io/Closeable;)V
    //   233: aload 9
    //   235: athrow
    //   236: astore 9
    //   238: aload 10
    //   240: astore 5
    //   242: goto -24 -> 218
    //   245: astore 9
    //   247: aload 11
    //   249: astore 6
    //   251: aload 10
    //   253: astore 5
    //   255: goto -37 -> 218
    //   258: astore 7
    //   260: aload 7
    //   262: astore 8
    //   264: goto -96 -> 168
    //   267: astore 15
    //   269: aload 15
    //   271: astore 8
    //   273: aload 10
    //   275: astore 5
    //   277: goto -109 -> 168
    //
    // Exception table:
    //   from	to	target	type
    //   99	106	154	java/io/IOException
    //   129	151	154	java/io/IOException
    //   71	88	216	finally
    //   168	198	216	finally
    //   88	99	236	finally
    //   99	106	245	finally
    //   129	151	245	finally
    //   71	88	258	java/io/IOException
    //   88	99	267	java/io/IOException
  }
}